home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / piconv < prev    next >
Encoding:
Text File  |  2012-12-11  |  7.2 KB  |  303 lines

  1. #!/usr/bin/perl
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.     if $running_under_some_shell;
  4. #!./perl
  5. # $Id: piconv,v 2.4 2009/07/08 13:34:15 dankogai Exp $
  6. #
  7. use 5.8.0;
  8. use strict;
  9. use Encode ;
  10. use Encode::Alias;
  11. my %Scheme =  map {$_ => 1} qw(from_to decode_encode perlio);
  12.  
  13. use File::Basename;
  14. my $name = basename($0);
  15.  
  16. use Getopt::Long qw(:config no_ignore_case);
  17.  
  18. my %Opt;
  19.  
  20. help()
  21.     unless
  22.       GetOptions(\%Opt,
  23.          'from|f=s',
  24.          'to|t=s',
  25.          'list|l',
  26.          'string|s=s',
  27.          'check|C=i',
  28.          'c',
  29.          'perlqq|p',
  30.          'htmlcref',
  31.          'xmlcref',
  32.          'debug|D',
  33.          'scheme|S=s',
  34.          'resolve|r=s',
  35.          'help',
  36.          );
  37.  
  38. $Opt{help} and help();
  39. $Opt{list} and list_encodings();
  40. my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
  41. defined $Opt{resolve} and resolve_encoding($Opt{resolve});
  42. $Opt{from} || $Opt{to} || help();
  43. my $from = $Opt{from} || $locale or help("from_encoding unspecified");
  44. my $to   = $Opt{to}   || $locale or help("to_encoding unspecified");
  45. $Opt{string} and Encode::from_to($Opt{string}, $from, $to) and print $Opt{string} and exit;
  46. my $scheme = do {
  47.     if (defined $Opt{scheme}) {
  48.     if (!exists $Scheme{$Opt{scheme}}) {
  49.         warn "Unknown scheme '$Opt{scheme}', fallback to 'from_to'.\n";
  50.         'from_to';
  51.     } else {
  52.         $Opt{scheme};
  53.     }
  54.     } else {
  55.     'from_to';
  56.     }
  57. };
  58.  
  59. $Opt{check} ||= $Opt{c};
  60. $Opt{perlqq}   and $Opt{check} = Encode::PERLQQ;
  61. $Opt{htmlcref} and $Opt{check} = Encode::HTMLCREF;
  62. $Opt{xmlcref}  and $Opt{check} = Encode::XMLCREF;
  63.  
  64. if ($Opt{debug}){
  65.     my $cfrom = Encode->getEncoding($from)->name;
  66.     my $cto   = Encode->getEncoding($to)->name;
  67.     print <<"EOT";
  68. Scheme: $scheme
  69. From:   $from => $cfrom
  70. To:     $to => $cto
  71. EOT
  72. }
  73.  
  74. my %use_bom = map { $_ => 1 } qw/UTF-16 UTF-32/;
  75.  
  76. # we do not use <> (or ARGV) for the sake of binmode()
  77. @ARGV or push @ARGV, \*STDIN;
  78.  
  79. unless ( $scheme eq 'perlio' ) {
  80.     binmode STDOUT;
  81.     my $need2slurp = $use_bom{ find_encoding($to)->name };
  82.     for my $argv (@ARGV) {
  83.         my $ifh = ref $argv ? $argv : undef;
  84.     $ifh or open $ifh, "<", $argv or warn "Can't open $argv: $!" and next;
  85.         $ifh or open $ifh, "<", $argv or next;
  86.         binmode $ifh;
  87.         if ( $scheme eq 'from_to' ) {    # default
  88.         if ($need2slurp){
  89.         local $/;
  90.         $_ = <$ifh>;
  91.         Encode::from_to( $_, $from, $to, $Opt{check} );
  92.         print;
  93.         }else{
  94.         while (<$ifh>) {
  95.             Encode::from_to( $_, $from, $to, $Opt{check} );
  96.             print;
  97.         }
  98.         }
  99.         }
  100.         elsif ( $scheme eq 'decode_encode' ) {    # step-by-step
  101.         if ($need2slurp){
  102.         local $/;
  103.         $_ = <$ifh>;
  104.                 my $decoded = decode( $from, $_, $Opt{check} );
  105.                 my $encoded = encode( $to, $decoded );
  106.                 print $encoded;
  107.         }else{
  108.         while (<$ifh>) {
  109.             my $decoded = decode( $from, $_, $Opt{check} );
  110.             my $encoded = encode( $to, $decoded );
  111.             print $encoded;
  112.         }
  113.         }
  114.     }
  115.     else {                                    # won't reach
  116.             die "$name: unknown scheme: $scheme";
  117.         }
  118.     }
  119. }
  120. else {
  121.  
  122.     # NI-S favorite
  123.     binmode STDOUT => "raw:encoding($to)";
  124.     for my $argv (@ARGV) {
  125.         my $ifh = ref $argv ? $argv : undef;
  126.     $ifh or open $ifh, "<", $argv or warn "Can't open $argv: $!" and next;
  127.         $ifh or open $ifh, "<", $argv or next;
  128.         binmode $ifh => "raw:encoding($from)";
  129.         print while (<$ifh>);
  130.     }
  131. }
  132.  
  133. sub list_encodings {
  134.     print join( "\n", Encode->encodings(":all") ), "\n";
  135.     exit 0;
  136. }
  137.  
  138. sub resolve_encoding {
  139.     if ( my $alias = Encode::resolve_alias( $_[0] ) ) {
  140.         print $alias, "\n";
  141.         exit 0;
  142.     }
  143.     else {
  144.         warn "$name: $_[0] is not known to Encode\n";
  145.         exit 1;
  146.     }
  147. }
  148.  
  149. sub help {
  150.     my $message = shift;
  151.     $message and print STDERR "$name error: $message\n";
  152.     print STDERR <<"EOT";
  153. $name [-f from_encoding] [-t to_encoding] [-s string] [files...]
  154. $name -l
  155. $name -r encoding_alias
  156.   -l,--list
  157.      lists all available encodings
  158.   -r,--resolve encoding_alias
  159.     resolve encoding to its (Encode) canonical name
  160.   -f,--from from_encoding  
  161.      when omitted, the current locale will be used
  162.   -t,--to to_encoding    
  163.      when omitted, the current locale will be used
  164.   -s,--string string         
  165.      "string" will be the input instead of STDIN or files
  166. The following are mainly of interest to Encode hackers:
  167.   -D,--debug          show debug information
  168.   -C N | -c           check the validity of the input
  169.   -S,--scheme scheme  use the scheme for conversion
  170. Those are handy when you can only see ascii characters:
  171.   -p,--perlqq
  172.   --htmlcref
  173.   --xmlcref
  174. EOT
  175.     exit;
  176. }
  177.  
  178. __END__
  179.  
  180. =head1 NAME
  181.  
  182. piconv -- iconv(1), reinvented in perl
  183.  
  184. =head1 SYNOPSIS
  185.  
  186.   piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
  187.   piconv -l
  188.   piconv [-C N|-c|-p]
  189.   piconv -S scheme ...
  190.   piconv -r encoding
  191.   piconv -D ...
  192.   piconv -h
  193.  
  194. =head1 DESCRIPTION
  195.  
  196. B<piconv> is perl version of B<iconv>, a character encoding converter
  197. widely available for various Unixen today.  This script was primarily
  198. a technology demonstrator for Perl 5.8.0, but you can use piconv in the
  199. place of iconv for virtually any case.
  200.  
  201. piconv converts the character encoding of either STDIN or files
  202. specified in the argument and prints out to STDOUT.
  203.  
  204. Here is the list of options.  Each option can be in short format (-f)
  205. or long (--from).
  206.  
  207. =over 4
  208.  
  209. =item -f,--from from_encoding
  210.  
  211. Specifies the encoding you are converting from.  Unlike B<iconv>,
  212. this option can be omitted.  In such cases, the current locale is used.
  213.  
  214. =item -t,--to to_encoding
  215.  
  216. Specifies the encoding you are converting to.  Unlike B<iconv>,
  217. this option can be omitted.  In such cases, the current locale is used.
  218.  
  219. Therefore, when both -f and -t are omitted, B<piconv> just acts
  220. like B<cat>.
  221.  
  222. =item -s,--string I<string>
  223.  
  224. uses I<string> instead of file for the source of text.
  225.  
  226. =item -l,--list
  227.  
  228. Lists all available encodings, one per line, in case-insensitive
  229. order.  Note that only the canonical names are listed; many aliases
  230. exist.  For example, the names are case-insensitive, and many standard
  231. and common aliases work, such as "latin1" for "ISO-8859-1", or "ibm850"
  232. instead of "cp850", or "winlatin1" for "cp1252".  See L<Encode::Supported>
  233. for a full discussion.
  234.  
  235. =item -C,--check I<N>
  236.  
  237. Check the validity of the stream if I<N> = 1.  When I<N> = -1, something
  238. interesting happens when it encounters an invalid character.
  239.  
  240. =item -c
  241.  
  242. Same as C<-C 1>.
  243.  
  244. =item -p,--perlqq
  245.  
  246. =item --htmlcref
  247.  
  248. =item --xmlcref
  249.  
  250. Applies PERLQQ, HTMLCREF, XMLCREF, respectively.  Try
  251.  
  252.   piconv -f utf8 -t ascii --perlqq
  253.  
  254. To see what it does.
  255.  
  256. =item -h,--help
  257.  
  258. Show usage.
  259.  
  260. =item -D,--debug
  261.  
  262. Invokes debugging mode.  Primarily for Encode hackers.
  263.  
  264. =item -S,--scheme scheme
  265.  
  266. Selects which scheme is to be used for conversion.  Available schemes
  267. are as follows:
  268.  
  269. =over 4
  270.  
  271. =item from_to
  272.  
  273. Uses Encode::from_to for conversion.  This is the default.
  274.  
  275. =item decode_encode
  276.  
  277. Input strings are decode()d then encode()d.  A straight two-step
  278. implementation.
  279.  
  280. =item perlio
  281.  
  282. The new perlIO layer is used.  NI-S' favorite.
  283.  
  284. You should use this option if you are using UTF-16 and others which
  285. linefeed is not $/.
  286.  
  287. =back
  288.  
  289. Like the I<-D> option, this is also for Encode hackers.
  290.  
  291. =back
  292.  
  293. =head1 SEE ALSO
  294.  
  295. L<iconv(1)>
  296. L<locale(3)>
  297. L<Encode>
  298. L<Encode::Supported>
  299. L<Encode::Alias>
  300. L<PerlIO>
  301.  
  302. =cut
  303.